import Base from 'models/base';

import SystemUtil from 'utils/systemUtil';

const codeFields = ['province', 'city', 'area', 'street'];

class orderGoods extends Base {
  constructor(goods) {
    super();

    this.defId = goods.goodsDefId;
    this.modelId = goods.goodsModelId;
    this.name = goods.goodsName;
    this.modelName = goods.goodsModelName;
    this.sourcePrice = goods.goodsModelSourcePrice;
    this.pic = this.getOnePic(goods.goodsPic);
    this.count = goods.buyCount;
    this.payPrice = goods.shouldPayPrice;
    this.buyMethod = goods.buyMethod;

    let type = goods.sourceSub;
    if (['YYPM', 'KJ', 'PT'].indexOf(type) === -1) {
      this.price = goods.goodsModelSalePrice;
    } else {
      this.price = goods.shouldPayPrice;
    }
  }
}

export default class PayOrder extends Base {
  constructor(data,orderId) {
    super();

    this.orderId = orderId;

    let address = data.address;
    if (address !== null) {
      this.buyerPhone = address.buyerPhone;
      this.buyerName = address.buyerName;

      let fullAddress = [];
      for (let i = 0; i < codeFields.length; i++) {
        const field = codeFields[i];
        let name = address[`${field}Name`];
        fullAddress.push(name);
      }
      this.buyerAddress = `${fullAddress.join('')} ${address.buyerAddress}`;
    } else {
      this.buyerPhone = '';
      this.buyerName = '';
      this.buyerAddress = '';
    }

    let goodsList = data.viewOrderGoods;
    let obj = goodsList[0];
    this.activityTypeId = obj.activityTypeId;

    let type = obj.sourceSub;
    this.type = type;

    let partnerName = obj.partnerName;
    let shopName = '';
    if (SystemUtil.isEmpty(partnerName)) {
      shopName = obj.branchName;
    } else {
      shopName = partnerName;
    }
    this.shopName = shopName;

    let goodsData = [];
    let totalPrice = 0;
    for (let i = 0; i < goodsList.length; i++) {
      let goods = goodsList[i];
      goods = new orderGoods(goods);
      goodsData.push(goods);
      totalPrice += goods.payPrice;
    }
    this.goodsList = goodsData;
    this.totalPrice = SystemUtil.roundValue(totalPrice, 2);
  }

  resetAddress(address){
    if (address === null) {
      this.buyerPhone = '';
      this.buyerName = '';
      this.buyerAddress = '';
      return;
    }

    this.buyerName = address.buyerName;
    this.buyerPhone = address.buyerPhone;
    this.buyerAddress = address.getFullAddress();
  }
}
